﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Common.Service;
using OC.Common.Entity;
using OC.Common.Interface;
using Common.BasePages;

namespace OC
{

    public partial class ProcessConfiguration : System.Web.UI.Page
    {
        private UserEntity _userInfo = null;

        public UserEntity UserInfo
        {
            get
            {
                if (_userInfo == null)
                {
                    BasePage basePage = new BasePage();
                    _userInfo = basePage.UserInfo as UserEntity;
                }
                return _userInfo;
            }
        }
        OC.Common.Interface.IDictionary _dictionaryService;

        public OC.Common.Interface.IDictionary DictionaryService
        {
            get
            {
                if (_dictionaryService == null)
                {
                    _dictionaryService = ServiceContainer.GetService<OC.Common.Interface.IDictionary>();
                }
                return _dictionaryService;
            }
        }

        IProcessConfig _processConfigService;

        public IProcessConfig ProcessConfigService
        {
            get
            {
                if (_processConfigService == null)
                {
                    _processConfigService = ServiceContainer.GetService<IProcessConfig>();
                }
                return _processConfigService;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitProcessTree();
                InitSetRemindTable();
                GetDepartmentInfo();
                //if(UserInfo == null)
                //    Response.Redirect("../Portal/Login.aspx");
            }
        }

        private List<ListItem> GetDepartmentInfo()
        {
            List<ListItem> liList = new List<ListItem>();
            IList organizations = DictionaryService.GetListByDictionaryType("DictionaryLogic_GetListByDictionaryType", "ORGANIZATION");
            foreach (DictionaryEntity item in organizations)
            {
                ListItem li = new ListItem();
                li.Text = item.CN_NAME;
                li.Value = item.DICTIONARY_ID;
                liList.Add(li);
            }
            ddlWorkListDepartment.DataSource = liList;
            ddlWorkListDepartment.DataBind();
            return liList;
        }

        private void InitSetRemindTable()
        {
            IList messageTypes = DictionaryService.GetListByDictionaryType("DictionaryLogic_GetListByDictionaryType", "MESSAGETYPE");
            Table table = new Table();
            table.ID = "remindtable";
            table.CssClass = "listTable3";
            foreach (DictionaryEntity item in messageTypes)
            {
                TableRow tr = new TableRow();
                TableCell td1 = new TableCell();
                td1.Text = item.CN_NAME;
                td1.ID = item.DICTIONARY_ID;
                TableCell td2 = new TableCell();
                td2.Controls.Add(GetRemindType());
                td2.ID = item.DICTIONARY_ID;
                tr.Cells.Add(td1);
                tr.Cells.Add(td2);
                table.Rows.Add(tr);
            }

            panelRemind.Controls.Add(table);
        }

        private void GetSendObject()
        {


        }

        private Control GetRemindType()
        {
            CheckBoxList checkboxes = new CheckBoxList();
            checkboxes.RepeatColumns = 2;
            checkboxes.Items.Add(new ListItem("邮件", "Email"));
            checkboxes.Items.Add(new ListItem("短信", "SMS"));
            checkboxes.Items.Add(new ListItem("IM", "IM"));
            checkboxes.Items.Add(new ListItem("平台内消息", "Message"));
            return checkboxes;
        }

        private void InitProcessTree()
        {
            IList processGroups = DictionaryService.GetListByDictionaryType("DictionaryLogic_GetListByDictionaryType", "PROCESS");
            tvProcess.Nodes.Clear();
            foreach (DictionaryEntity item in processGroups)
            {
                if (item.PARENT_DICTIONARY_ID.Trim() == "0")//先找到根节点
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = item.CN_NAME;
                    tn.Value = item.DICTIONARY_ID;
                    GetChildNodeByList(processGroups, item.DICTIONARY_ID, tn);
                    tvProcess.Nodes.Add(tn);
                }
            }
            tvProcess.ExpandDepth = 1;
        }

        /// <summary>
        /// 根据ID在查询到的组织数据中轮询查找它的子节点
        /// </summary>
        /// <param name="structure"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private TreeNode GetChildNodeByList(IList structure, string id, TreeNode ptn)
        {
            foreach (DictionaryEntity item in structure)
            {
                if (id == item.PARENT_DICTIONARY_ID)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = item.CN_NAME;
                    tn.Value = item.DICTIONARY_ID;
                    ptn.ChildNodes.Add(tn);
                    foreach (DictionaryEntity citem in structure)
                    {
                        if (item.DICTIONARY_ID == citem.PARENT_DICTIONARY_ID)
                        {
                            TreeNode ctn = new TreeNode();
                            ctn.Text = citem.CN_NAME;
                            ctn.Value = citem.DICTIONARY_ID;
                            tn.ChildNodes.Add(ctn);
                        }
                    }
                }
            }
            return ptn;
        }

        protected void tvProcess_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            TreeNode tn = e.Node;
            if (tn.ChildNodes.Count > 0)
            {
                foreach (TreeNode item in tn.ChildNodes)
                {
                    if (item.ChildNodes.Count <= 0)
                    {
                        IList dicEntitys = DictionaryService.GetList(item.Value);
                        foreach (DictionaryEntity dics in dicEntitys)
                        {
                            TreeNode ctn = new TreeNode();
                            ctn.Text = dics.CN_NAME;
                            ctn.Value = dics.DICTIONARY_ID;
                            item.ChildNodes.Add(ctn);
                        }
                    }
                }
            }
        }

        protected void btnPermissionsSave_Click(object sender, EventArgs e)
        {
            if (tvProcess.SelectedNode != null && (tvProcess.SelectedNode.Depth == 1 || tvProcess.SelectedNode.Depth == 2))
            {
                string processName = tvProcess.SelectedNode.Parent.Text;
                string stepName = tvProcess.SelectedNode.Text;
                ProcessConfigEntity pcEntity = ProcessConfigService.GetProcessConfig(processName, stepName);
                if (pcEntity != null)
                {//存在 则update
                    GetProcessConfigEntity(pcEntity, "Permissions");
                    pcEntity.UPDATE_BY = "";// UserInfo.USER_ID;
                    pcEntity.UPDATE_DATE = DateTime.Now;
                    ProcessConfigService.UpdateProcessConfig(pcEntity);
                }
                else
                {//不存在 则insert
                    pcEntity = new ProcessConfigEntity();
                    GetProcessConfigByPermissions(pcEntity);
                    pcEntity.CONFIG_ID = Guid.NewGuid().ToString();
                    pcEntity.PROCESS_NAME = processName;
                    pcEntity.STEP_NAME = stepName;
                    pcEntity.CREATE_BY = "";//UserInfo.USER_ID;
                    pcEntity.CREATE_DATE = DateTime.Now;
                    ProcessConfigService.InsertProcessConfig(pcEntity);
                }
            }
        }

        private void GetProcessConfigEntity(ProcessConfigEntity pcEntity, string module)
        {
            if (pcEntity != null)
            {
                switch (module)
                {
                    case "Permissions":
                        GetProcessConfigByPermissions(pcEntity);
                        break;
                }
            }
            else
            {

            }
        }

        private void GetProcessConfigByPermissions(ProcessConfigEntity pcEntity)
        {
            string scanFormView = string.Empty;
            if (cbScanAll.Checked)
                scanFormView += "0,";
            if (cbScanTransactor.Checked)
                scanFormView += "1,";
            if (cbScanSection.Checked)
                scanFormView += "2,";
            pcEntity.FORM_VIEW = scanFormView;
            if (cbScanUser.Checked)
                pcEntity.FORM_VIEWUSER = txtScanUser.Text;
            else
            {
                pcEntity.FORM_VIEWUSER = string.Empty;
                txtScanUser.Text = string.Empty;
            }

            string downloadFormView = string.Empty;
            if (cbDownloadAll.Checked)
                downloadFormView += "0,";
            if (cbDownloadTransactor.Checked)
                downloadFormView += "1,";
            if (cbDownloadSection.Checked)
                downloadFormView += "2,";
            pcEntity.ATTACHMENT_DOWNLOAD = downloadFormView;
            if (cbDownloadUser.Checked)
                pcEntity.ATTACHMENT_DOWNLOADUSER = txtDownloadUser.Text;
            else
            {
                pcEntity.ATTACHMENT_DOWNLOADUSER = string.Empty;
                txtDownloadUser.Text = string.Empty;
            }
        }

        protected void tvProcess_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (tvProcess.SelectedNode != null && (tvProcess.SelectedNode.Depth == 1 || tvProcess.SelectedNode.Depth == 2))
            {
                string processName = tvProcess.SelectedNode.Parent.Text;
                string stepName = tvProcess.SelectedNode.Text;
                ProcessConfigEntity pcEntity = ProcessConfigService.GetProcessConfig(processName, stepName);
                SetProcessConfigByPermissions(pcEntity);
            }
        }

        private void SetProcessConfigByPermissions(ProcessConfigEntity pcEntity)
        {
            if (pcEntity != null)
            {
                if (pcEntity.FORM_VIEW.IndexOf('0') >= 0)
                    cbScanAll.Checked = true;
                if (pcEntity.FORM_VIEW.IndexOf('1') >= 0)
                    cbScanTransactor.Checked = true;
                if (pcEntity.FORM_VIEW.IndexOf('2') >= 0)
                    cbScanSection.Checked = true;
                if (!string.IsNullOrEmpty(pcEntity.FORM_VIEWUSER))
                {
                    cbScanUser.Checked = true;
                    txtScanUser.Text = pcEntity.FORM_VIEWUSER;
                }

                if (pcEntity.ATTACHMENT_DOWNLOAD.IndexOf('0') >= 0)
                    cbDownloadAll.Checked = true;
                if (pcEntity.ATTACHMENT_DOWNLOAD.IndexOf('1') >= 0)
                    cbDownloadTransactor.Checked = true;
                if (pcEntity.ATTACHMENT_DOWNLOAD.IndexOf('2') >= 0)
                    cbDownloadSection.Checked = true; 
                if (!string.IsNullOrEmpty(pcEntity.ATTACHMENT_DOWNLOADUSER))
                {
                    cbDownloadUser.Checked = true;
                    txtDownloadUser.Text = pcEntity.ATTACHMENT_DOWNLOADUSER;
                }
            }
            else
            {
                cbScanAll.Checked = false;
                cbScanTransactor.Checked = false;
                cbScanSection.Checked = false;
                cbDownloadAll.Checked = false;
                cbDownloadTransactor.Checked = false;
                cbDownloadSection.Checked = false;
                cbDownloadUser.Checked = false;
                cbScanUser.Checked = false;
                txtScanUser.Text = txtDownloadUser.Text = string.Empty;

            }
        }
    }
}